<template>
  <div
    class="transfer-wrap"
    v-loading="loading"
    :element-loading-text="loadingText"
    element-loading-spinner="el-icon-loading"
    element-loading-background="rgba(255, 255, 255, .8)"
  >
    <div class="tags">
      <div class="tags-line" :class="{'tags-line-active':!typeFlag}"></div>
      <div
        class="tags-item tags-item-line"
        :class="{'tags-active':typeFlag}"
        @click="typeSwitch(true)"
      >
        <span>转入</span>
      </div>
      <div class="tags-item" :class="{'tags-active':!typeFlag}" @click="typeSwitch(false)">
        <span>转出</span>
      </div>
    </div>

    <div class="transfer-content">
      <div class="wallet-box">
        <div class="wallet-item">
          <p>代理钱包可用余额</p>
          <p>
            <count-to
              :start-val="0"
              :decimals="2"
              :end-val="+agencyMoney || 0"
              :duration="300"
              class="card-panel-num"
            />
            <span>元</span>
          </p>
        </div>
        <div class="img">
          <img v-if="typeFlag" src="@/assets/capital/transfer-icon1.png" alt />
          <img v-else src="@/assets/capital/transfer-icon2.png" alt />
        </div>
        <div class="wallet-item">
          <p>{{walletTxt + '可用余额'}}</p>
          <p>
            <count-to
              :start-val="0"
              :decimals="2"
              :end-val="+otherWallet.money || 0"
              :duration="300"
              class="card-panel-num"
            />
            <span>元</span>
          </p>
          <el-popover
            class="select-wrap"
            placement="bottom-end"
            width="195"
            trigger="click"
            v-model="visible"
            :popper-options="{ boundariesElement: 'viewport', removeOnDestroy: true }"
          >
            <div class="select-box">
              <div
                class="select-item"
                v-for="(item,index) in walletlist"
                :key="index"
                @click="walletSwitch(item,index)"
              >
                <i v-show="walletFlag==index" class="line"></i>
                <p v-if="item.type == 'ordinary'">普通钱包</p>
                <p v-else-if="item.type == 'special'">精选钱包</p>
                <p v-else-if="item.type == 'seller'">优选钱包</p>
                <span :class="{'select-active':walletFlag==index}">余额 {{item.usable_money}}元</span>
              </div>
            </div>
            <div class="select-btn" slot="reference">
              <img
                v-if="!visible"
                class="select-img"
                src="../../../assets/capital/transfer-icon3.png"
                alt
              />
              <img v-else class="select-img" src="../../../assets/capital/transfer-icon4.png" alt />
            </div>
          </el-popover>
        </div>
      </div>

      <div class="form-box">
        <el-form>
          <el-form-item :label="typeFlag ? '转入金额（元）：' : '转出金额（元）：' ">
            <el-input v-model="reqParams.money" @keyup.native="keyupEvent($event,reqParams.money)"></el-input>
            <span class="total-btn" @click="totalHandle">全部</span>
          </el-form-item>
        </el-form>
      </div>

      <div class="btn-box" @click="transferHandle">
        <span
          v-if="typeFlag"
          :class="{'btn-color':+reqParams.money && +reqParams.money <= +otherWallet.money }"
        >确认转入</span>
        <span
          v-else
          :class="{'btn-color':+reqParams.money && +reqParams.money <= +agencyMoney }"
        >确认转出</span>
      </div>
    </div>

    <div class="tips">
      <p>
        <span>*</span>温馨提示
      </p>
      <p>精选钱包与代理钱包互转免手续费</p>
    </div>
  </div>
</template>

<script>
import CountTo from "vue-count-to";
import { getWalletlist, transfer } from "@/api/capital";
export default {
  components: {
    CountTo,
  },
  data() {
    return {
      visible: false,
      loading: false,
      loadingText: "正在转入",
      typeFlag: true,
      walletFlag: 0,
      capitalData: {},
      walletlist: [],
      reqParams: {
        money: "",
        type: "",
        wallet: "",
      },
      walletTxt: "其它钱包",
      agencyMoney: "",
      otherWallet: {
        type: "",
        money: "",
      },
    };
  },
  created() {
    this._getWalletlist();
  },
  methods: {
    typeSwitch(flag) {
      if (this.typeFlag != flag) {
        this.typeFlag = !this.typeFlag;
        this.loadingText = this.typeFlag ? "正在转入" : "正在转出";
        this.reqParams.money = "";
      }
    },
    keyupEvent(e, input) {
      e.target.value = e.target.value.replace(/[^\d.]/g, "");
      e.target.value = e.target.value.replace(/\.{2,}/g, ".");
      e.target.value = e.target.value.replace(/^\./g, "0.");
      e.target.value = e.target.value.replace(
        /^\d*\.\d*\./g,
        e.target.value.substring(0, e.target.value.length - 1)
      );
      e.target.value = e.target.value.replace(/^0[^\.]+/g, "0");
      e.target.value = e.target.value.replace(/^(\d+)\.(\d\d).*$/, "$1.$2");
      this.reqParams.money = e.target.value;
      console.log(this.reqParams.money);
    },
    fHandle() {
      this.typeFlag = true;
      this.reqParams.money = ''
      this._getWalletlist();
    },
    // 全部
    totalHandle() {
      this.reqParams.money = this.typeFlag
        ? this.otherWallet.money
        : this.agencyMoney;

      console.log(this.reqParams);
    },
    // 选择钱包
    walletSwitch(item, index) {
      this.walletFlag = index;
      this.otherWallet.type = item.type;
      this.otherWallet.money = item.usable_money;
      this.visible = false;
    },
    // 确认转入/转出
    transferHandle() {
      this.reqParams.type = this.typeFlag ? "TRANSFER_IN" : "TRANSFER_OUT";
      this.reqParams.wallet = this.otherWallet.type;
      console.log(this.typeFlag ? "转入参数" : "转出参数", this.reqParams);
      if (this.typeFlag && +this.otherWallet.money == 0) {
        this.$message.error(this.walletTxt + "余额不足");
      } else if (!this.typeFlag && +this.agencyMoney == 0) {
        this.$message.error("代理钱包余额不足");
      } else if (!+this.reqParams.money) {
        this.$message.error(
          this.typeFlag ? "请输入转入金额" : "请输入转出金额"
        );
      } else if (
        +this.reqParams.money > +this.otherWallet.money &&
        this.typeFlag
      ) {
        this.$message.error(this.walletTxt + "可用余额不足");
      } else if (+this.reqParams.money > +this.agencyMoney && !this.typeFlag) {
        this.$message.error("代理钱包可用余额不足");
      } else {
        this.loading = true;
        transfer(this.reqParams)
          .then((res) => {
            if (res.code == 1) {
              this.$message.success(this.typeFlag ? "转入成功" : "转出成功");
              this.reqParams.money = "";
              this.typeFlag = true;
              this.$emit("transferClose");
              //   setTimeout(() => {
              //     this.reqParams.money = "";
              //     this.typeFlag = true;
              //     this.$emit("transferClose");
              //   }, 1000);
            } else {
              this.$message.error(
                res.mag || this.typeFlag
                  ? "转入失败，请重试"
                  : "转出失败，请重试"
              );
            }
          })
          .finally(() => {
            this.loading = false;
          });
      }
    },
    // 获取钱包列表
    _getWalletlist() {
      this.walletlist = [];
      getWalletlist().then((res) => {
        let arr = [];
        if (res.code == 1) {
          this.agencyMoney = res.data.agency.usable_money;
          for (let i in res.data.wallet_list) {
            console.log(i);
            this.walletlist.push(res.data.wallet_list[i]);
            arr.push(i);
          }
          for (let i = 0; i < arr.length; i++) {
            this.walletlist[i].type = arr[i];
          }
          // this.walletlist = res.data;
          console.log("钱包列表", this.walletlist);
          this.reqParams.wallet = this.walletlist[0].type;
          this.otherWallet.type = this.walletlist[0].type;
          this.otherWallet.money = this.walletlist[0].usable_money;
          if (this.otherWallet.type == "ordinary") {
            this.walletTxt = "普通钱包";
          } else if (this.otherWallet.type == "special") {
            this.walletTxt = "精选钱包";
          } else if (this.otherWallet.type == "seller") {
            this.walletTxt = "优选钱包";
          }
        }
      });
    },
  },
};
</script>
<style lang="scss">
.transfer-wrap {
  .form-box {
    .el-form {
      .el-input {
        width: 300px;
      }
    }
  }
}
</style>
<style lang='scss' scoped>
.transfer-wrap {
  .tags {
    width: 100%;
    height: 40px;
    line-height: 40px;
    display: flex;
    justify-content: space-between;
    position: relative;
    background: #fff;
    margin-top: 20px;
    .tags-line {
      width: 50%;
      position: absolute;
      left: 0;
      bottom: 0;
      transition: all 0.3s;
      &.tags-line-active {
        left: 50%;
      }
      &::after {
        content: "";
        width: 105px;
        height: 2px;
        background: #fe6018;
        border-radius: 2px;
        position: absolute;
        top: 50%;
        left: 50%;
        transform: translate(-50%, -50%);
      }
    }
    .tags-item {
      width: 50%;
      font-size: 14px;
      text-align: center;
      box-sizing: border-box;
      color: #9a9a9a;
      font-weight: bold;
      &.tags-item-line {
        position: relative;
        box-sizing: border-box;
        &::after {
          content: "";
          position: absolute;
          width: 1px;
          height: 40px;
          background: #ebf0f2;
          top: 50%;
          right: 0;
          transform: translateY(-50%);
        }
      }
      &.tags-active {
        color: #fe6018;
      }
      span {
        display: inline-block;
        width: 50px;
        height: 100%;
        cursor: pointer;
        &:hover {
          color: #fe6018;
        }
      }
    }
  }
  .transfer-content {
    margin: 26px 0 60px 0;
    .wallet-box {
      width: 100%;
      background: #f8f9fb;
      display: flex;
      justify-content: space-between;
      text-align: center;
      padding: 25px 70px;
      .wallet-item {
        position: relative;
        p {
          &:first-of-type {
            color: #ababab;
          }
          &:last-of-type {
            font-size: 22px;
            color: #121212;
            font-weight: bold;
          }
        }
      }
      .img {
        position: relative;
        img {
          width: 28px;
          height: 28px;
          position: absolute;
          top: 50%;
          left: 50%;
          transform: translate(-50%, -50%);
        }
      }
      .select-wrap {
        position: absolute;
        top: 60%;
        right: -50px;
        transform: translateY(-50%);
        .select-btn {
          width: 40px;
          height: 10px;
          cursor: pointer;
          position: relative;
          .select-img {
            width: 17px;
            height: 10px;
            position: relative;
            left: 1px;
            bottom: 18px;
          }
        }
      }
    }
    .form-box {
      padding-left: 35px;
      margin-top: 30px;
      .total-btn {
        font-size: 14px;
        color: #ff805c;
        cursor: pointer;
        margin-left: 19px;
      }
    }

    .btn-box {
      width: 340px;
      height: 55px;
      text-align: center;
      line-height: 55px;
      margin: 0 auto;
      margin-top: 40px;
      font-size: 16px;
      color: #fffefe;
      cursor: pointer;
      span {
        display: inline-block;
        width: 100%;
        height: 100%;
        background: #eaeaea;
        &.btn-color {
          background: #ff805c;
        }
      }
    }
  }
  .tips {
    letter-spacing: 2px;
    padding-bottom: 40px;
    P {
      &:first-of-type {
        color: #111;
        span {
          color: red;
          padding-right: 10px;
        }
      }
      &:last-of-type {
        color: #9a9a9a;
        font-size: 12px;
      }
    }
  }
}
.select-box {
  .select-item {
    border-bottom: 1px solid #ebf0f2;
    padding: 0 0 10px 5px;
    cursor: pointer;
    position: relative;
    &:last-of-type {
      border: none;
    }
    &:first-of-type {
      border-bottom: 1px solid #ebf0f2;
    }
    .line {
      position: absolute;
      display: inline-block;
      width: 2px;
      height: 22px;
      background: #ff7f5b;
      top: -11px;
      left: -12px;
    }
    p {
      line-height: 0;
    }
    span {
      color: #999999;
      &.select-active {
        color: #ff805c;
      }
    }
  }
}
</style>